package com.ssm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ssm.entity.Student;
import com.ssm.service.ClassesService;
import com.ssm.service.StudentService;
import com.ssm.utils.R;
import com.ssm.vo.StudentVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")

public class StudentController {
    private final StudentService studentService;
    private final ClassesService classesService;

    public StudentController(StudentService studentService, ClassesService classesService) {
        this.studentService = studentService;
        this.classesService = classesService;
    }
    @GetMapping("/getStudent/{id}")
    public Student getStudent(@PathVariable Long id) {
        Student student = studentService.getById(id);
        if(student==null||student.getIsDeleted() == 1)
            return null;
        return student;
    }

    @PostMapping("/conditionFindByPage")
    public IPage<Student> conditionFindByPage(Integer page, Integer pageSize, @RequestBody StudentVo studentVo) {
        return studentService.conditionFindByPage(page, pageSize, studentVo);
    }

    @PostMapping("/addStudent")
    public R addStudent(@RequestBody Student student) {
        student.setCname(classesService.getById(student.getCid()).getCname());
        return studentService.save(student) ? R.ok().message("添加成功！"): R.error().message("添加失败！");
    }
    @PostMapping("/updateStudent")
    public R updateStudent(@RequestBody Student student) {
        student.setIsDeleted(0);
        return studentService.updateById(student) ? R.ok().message("修改成功！"): R.error().message("修改失败！");
    }
    @PostMapping("/batchDeleteStudent")
    public R batchDeleteStudent(@RequestBody List<Long> ids) {
        return studentService.removeByIds(ids) ? R.ok().message("删除成功！"): R.error().message("删除失败！");
    }

}
